<?php
namespace app\blog\controller;
use app\common\controller;
class Article extends controller\AdminCommon
{
    /*
     * 文章列表
     */
    public function index()
    {
        $list = db('article')->select();
        $this->assign("list",$list);
        return $this->fetch();
    }
    /*
     * 添加文章
     */
    public function add()
    {
        if($_POST){
            $post = $_POST;
            $data['title'] = $post['title'];
            $data['content'] = $post['editorValue'];
            $data['writer'] = $post['writer'];
            $data['type'] = $post['type'];
            $data['status'] = $post['status']=='on'?1:0;
            $data['tabs'] = implode(',',$post['tabs']);
            $data['hot'] = implode(',',$post['hot']);
            if(!empty($post['id'])){
                $sta = db('article')->where(array('id'=>$post['id']))->update($data);
            }else{
                $sta = db('article')->insert($data);
            }
            if($sta){
                return json(array(
                    'status'=>200,
                    'msg'=>"编辑成功",
                    'tips'=>"1",
                    'close'=>"on",
                    'refresh'=>1,
                ));
            }else{
                return json(array(
                    'status'=>40001,
                    'msg'=>"未知错误，请联系管理员",
                    'refresh'=>0,
                    'tips'=>1
                ));
            }
        }else{
            $type = db('article_type')->select();
            $this->assign("type",$type);
            $tabs = db('article_tabs')->select();
            $this->assign("tabs",$tabs);
            $id = input('id');
            if(!empty($id)){
                $info = db('article')->where(array('id'=>$id))->find();
                $this->assign("info",$info);
            }
            return $this->fetch();
        }
    }
    /*
     * 删除列表
     */
    public function del(){
        $id = input('id');
        if($id){
            $sta = db('article')->where(array('id'=>$id))->delete();
            if($sta){
                return json(array(
                    'status'=>200,
                    'msg'=>"删除成功",
                    'tips'=>"1",
                    'refresh'=>1,
                ));
            }else{
                return json(array(
                    'status'=>40001,
                    'msg'=>"未知错误，请联系管理员",
                    'refresh'=>0,
                    'tips'=>1
                ));
            }
        }else{
            return json(array(
                'status'=>40001,
                'msg'=>"参数错误",
                'refresh'=>0,
                'tips'=>1
            ));
        }
    }
    /*
     * 类别列表
     */
    public function type()
    {
        $list = db('article_type')->select();
        $this->assign("list",$list);
        return $this->fetch();
    }
    /*
     * 添加类别
     */
    public function addtype()
    {
        if($_POST){
            $post = $_POST;
            $data = $post;
            if(!empty($post['id'])){
                $sta = db('article_type')->where(array('id'=>$post['id']))->update($data);
            }else{
                $sta = db('article_type')->insert($data);
            }
            if($sta){
                return json(array(
                    'status'=>200,
                    'msg'=>"编辑成功",
                    'tips'=>"1",
                    'close'=>"on",
                    'refresh'=>1,
                ));
            }else{
                return json(array(
                    'status'=>40001,
                    'msg'=>"未知错误，请联系管理员",
                    'refresh'=>0,
                    'tips'=>1
                ));
            }
        }else{
            if(!empty($_GET['id'])){
                $id = $_GET['id'];
                $info = db('article_type')->where(array('id'=>$id))->find();
                if($info){
                    $this->assign("info",$info);
                }
            }
            return $this->fetch();
        }
    }
    /*
     * 删除类别
     */
    public function deltype(){
        $id = input('id');
        if($id){
            $sta = db('article_type')->where(array('id'=>$id))->delete();
            if($sta){
                return json(array(
                    'status'=>200,
                    'msg'=>"删除成功",
                    'tips'=>"1",
                    'refresh'=>1,
                ));
            }else{
                return json(array(
                    'status'=>40001,
                    'msg'=>"未知错误，请联系管理员",
                    'refresh'=>0,
                    'tips'=>1
                ));
            }
        }else{
            return json(array(
                'status'=>40001,
                'msg'=>"参数错误",
                'refresh'=>0,
                'tips'=>1
            ));
        }
    }
    /*
     *  标签列表
     */
    public function tabs()
    {
        $list = db('article_tabs')->select();
        $this->assign("list",$list);
        return $this->fetch();
    }
    /*
     *  标签管理
     */
    public function addtabs()
    {
        if($_POST){
            $post = $_POST;
            $data = $post;
            if(!empty($post['id'])){
                $sta = db('article_tabs')->where(array('id'=>$post['id']))->update($data);
            }else{
                $sta = db('article_tabs')->insert($data);
            }
            if($sta){
                return json(array(
                    'status'=>200,
                    'msg'=>"编辑成功",
                    'tips'=>"1",
                    'close'=>"on",
                    'refresh'=>1,
                ));
            }else{
                return json(array(
                    'status'=>40001,
                    'msg'=>"未知错误，请联系管理员",
                    'refresh'=>0,
                    'tips'=>1
                ));
            }
        }else{
            if(!empty($_GET['id'])){
                $id = $_GET['id'];
                $info = db('article_tabs')->where(array('id'=>$id))->find();
                if($info){
                    $this->assign("info",$info);
                }
            }
            return $this->fetch();
        }
    }
    /*
     * 删除标签
     */
    public function deltabs(){
        $id = input('id');
        if($id){
            $sta = db('article_tabs')->where(array('id'=>$id))->delete();
            if($sta){
                return json(array(
                    'status'=>200,
                    'msg'=>"删除成功",
                    'tips'=>"1",
                    'refresh'=>1,
                ));
            }else{
                return json(array(
                    'status'=>40001,
                    'msg'=>"未知错误，请联系管理员",
                    'refresh'=>0,
                    'tips'=>1
                ));
            }
        }else{
            return json(array(
                'status'=>40001,
                'msg'=>"参数错误",
                'refresh'=>0,
                'tips'=>1
            ));
        }
    }
}
